<template>
  <div class="reward-list">
    <el-card>
      <div class="toolbar">
        <div class="search-area">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索学号、姓名"
            style="width: 200px"
            clearable
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
          
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
        </div>
      </div>
      
      <el-table :data="rewardList" v-loading="loading" stripe style="margin-top: 20px">
        <el-table-column prop="studentId" label="学号" width="120" />
        <el-table-column prop="type" label="类型" width="150" />
        <el-table-column prop="name" label="名称" width="200" />
        <el-table-column prop="level" label="级别" width="100" />
        <el-table-column prop="awardDate" label="获奖日期" width="120" />
        <el-table-column prop="description" label="说明" />
        <el-table-column prop="createdAt" label="录入时间" width="180" />
      </el-table>
      
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSearch"
        @current-change="handleSearch"
        style="margin-top: 20px; justify-content: flex-end"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { Reward } from '@/types'
import { getRewardList } from '@/api/reward'

const loading = ref(false)
const rewardList = ref<Reward[]>([])
const total = ref(0)
const searchKeyword = ref('')

const pagination = reactive({
  page: 1,
  pageSize: 20
})

const fetchRewardList = async () => {
  loading.value = true
  try {
    const res = await getRewardList(pagination)
    rewardList.value = res.list
    total.value = res.total
  } catch (error) {
    console.error('获取奖励列表失败:', error)
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  pagination.page = 1
  fetchRewardList()
}

onMounted(() => {
  fetchRewardList()
})
</script>

<style scoped>
.reward-list {
  padding: 20px;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 15px;
}

.search-area {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
</style>

